/**
 * 
 */
package operation;

import java.util.HashMap;

import bean.User;

import common.Utils;

import data.DaoFactory;
import data.DaoFactory.DaoName;
import data.IUserDao;

/**
 * Initialize System Users.
 * 
 * @author chuxiaoyuan
 *
 */
public class InitializeUsers extends Deploy {

	/**
	 * Constructor.
	 */
	public InitializeUsers() {
		super();
	}

	/* (non-Javadoc)
	 * @see deploy.Deploy#getDescription()
	 */
	@Override
	public String getDescription() {
		return "Initialize Users for System.";
	}

	/* (non-Javadoc)
	 * @see deploy.Deploy#run()
	 */
	@Override
	public boolean run() throws Exception {
		
		// Add default system administrator.
		DaoFactory factory = DaoFactory.getDaoFactory();
		IUserDao userDao = factory.getDao(DaoName.UserDao);
		
		// The System Admin
		final String account = "SysAdmin";
		final String password = Utils.encryptPassword("2HuCBtHj@gEq");
		
		// Test if already initialized.
		@SuppressWarnings("serial")
		User user = userDao.selectByPrimaryKey(new HashMap<String, Object>() {
			{
				put("account", account);
			}
		});
		if (user != null) {
			return true;
		}

		// Add default damin.
		User admin = new User();
		admin.setAccount(account);
		admin.setPassword(password);
		admin.setCustomerid(null);
		if (userDao.insert(admin) > 0) {
			return true;
		}
		
		return false;
	}
}
